<?php

namespace apiend\modules\h5\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\helpers\DateHelper;

/**
 * 学校精品课程模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SchoolCourseSearch extends \common\models\course\Course {

    /**
     * 学校精品课程
     * @param array $id 学校ID
     * @return ActiveDataProvider
     */
    public function search($id) {
        $query = static::find()
        ->where([
            'status' => static::STATUS_ACTIVE,
            'school_id' => $id
        ])
        ->orderBy(['created_at' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        Yii::$app->db->cache(function($db) use($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id',
            'name',
            'thumb',
            'begintime' => function ($model) {
                return date('Y-m-d', $model->begin_time);
            },
            'teacher' => function ($model) {
                if ($model->teacher) {
                    return $model->teacher->name;
                }
                return '待定';
            },
            'price',
            'finished',
        ];
    }

}
